// Author: wsfuyibing <682805@qq.com>
// Date: 2025-04-27

package config

import (
	"gitee.com/go-libs/config"
	"time"
)

// Upload
// 上传配置实例.
var Upload UploadConfig

// 默认配置.

const (
	defaultUploadBasePath  = "./upload"
	defaultUploadFolder    = "2006-01-02"
	defaultUploadSeparator = "2006-01"
)

type (
	// UploadConfig
	// 上传配置接口.
	UploadConfig interface {
		GetBasePath() string
		GetSeparator(tm time.Time) string
		GetFolder(tm time.Time) string
		GetHost() string
	}

	uploadConfig struct {
		BasePath  string `yaml:"base-path"`
		Separator string `yaml:"separator"`
		Folder    string `yaml:"folder"`
		Host      string `yaml:"host"`
	}
)

// +---------------------------------------------------------------------------+
// | Interface methods                                                         |
// +---------------------------------------------------------------------------+

func (o *uploadConfig) GetBasePath() string              { return o.BasePath }
func (o *uploadConfig) GetSeparator(tm time.Time) string { return tm.Format(o.Separator) }
func (o *uploadConfig) GetFolder(tm time.Time) string    { return tm.Format(o.Folder) }
func (o *uploadConfig) GetHost() string                  { return o.Host }

// +---------------------------------------------------------------------------+
// | Hook methods                                                              |
// +---------------------------------------------------------------------------+

func (o *uploadConfig) After() {
	if o.BasePath == "" {
		o.BasePath = defaultUploadBasePath
	}
	if o.Folder == "" {
		o.Folder = defaultUploadFolder
	}
	if o.Separator == "" {
		o.Separator = defaultUploadSeparator
	}
}

// +---------------------------------------------------------------------------+
// | Access methods                                                            |
// +---------------------------------------------------------------------------+

func (o *uploadConfig) init() *uploadConfig {
	_ = config.Seek("upload.yaml", "upload.yml").ScanYaml(o)
	return o
}
